package huy.mmt.gamelathinh;

import huy.mmt.gamelathinh.config.Config;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

/**
 * Hiá»‡n thá»‹ pháº§n cÃ i Ä‘áº·t. Chá»�n theme, táº¯t báº­t nháº¡c ná»�n, Ã¢m thanh
 * 
 * @author VAN GIOI
 * 
 */
public class Setting extends MyApp implements OnClickListener {
	public static boolean isSound = true;
	public static boolean isMusic = true;

	Button button_back;
	CheckBox check_music, check_sound, check_fruit, check_trans, check_animal,
			check_math, check_de, check_trungbinh, check_kho;
	

	public static String[] nameTheme = new String[] { "Fruit", "Trans",
			"Animal", "Math" };

	MySharedPreferences mySharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_setting);

		mySharedPreferences = new MySharedPreferences(this);
		mySharedPreferences.getIsSound();
		mySharedPreferences.getIsMusic();
		mySharedPreferences.getThemes();
		mySharedPreferences.getDo_kho();

		button_back = (Button) findViewById(R.id.bt_back);
		button_back.setOnClickListener(this);

		

		check_music = (CheckBox) findViewById(R.id.check_mussic);
		check_music.setOnClickListener(this);
		check_music.setChecked(isMusic);

		check_sound = (CheckBox) findViewById(R.id.check_sound);
		check_sound.setOnClickListener(this);
		check_sound.setChecked(isSound);

		// Select themes
		check_fruit = (CheckBox) findViewById(R.id.check_fruit);
		check_fruit.setOnClickListener(this);

		check_trans = (CheckBox) findViewById(R.id.check_trans);
		check_trans.setOnClickListener(this);

		check_animal = (CheckBox) findViewById(R.id.check_animal);
		check_animal.setOnClickListener(this);

		check_math = (CheckBox) findViewById(R.id.check_math);
		check_math.setOnClickListener(this);

		// Chinh Ä‘á»™ khÃ³
		check_de = (CheckBox) findViewById(R.id.check_de);
		check_de.setOnClickListener(this);

		check_trungbinh = (CheckBox) findViewById(R.id.check_trungbinh);
		check_trungbinh.setOnClickListener(this);

		check_kho = (CheckBox) findViewById(R.id.check_kho);
		check_kho.setOnClickListener(this);

		setThemes(Config.THEMES);
		setDo_kho(Config.Do_kho);
	}

	@Override
	public void onClick(View v) {
		Main.click.start();
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;
		case R.id.check_mussic:
			isMusic = check_music.isChecked();
			mySharedPreferences.updateIsMusic(isMusic);
			break;
		case R.id.check_sound:
			isSound = check_sound.isChecked();
			mySharedPreferences.updateIsSound(isSound);
			break;
		// Set themes
		case R.id.check_fruit:
			setThemes(1);
			break;		
		case R.id.check_trans:
			setThemes(2);
			break;
		case R.id.check_animal:
			setThemes(3);
			break;			
		case R.id.check_math:
			setThemes(4);
			break;
		// Set do kho
		case R.id.check_de:
			setDo_kho(1);
			break;
		case R.id.check_trungbinh:
			setDo_kho(2);
			break;
		case R.id.check_kho:
			setDo_kho(3);
			break;

		default:
			break;
		}
	}

	public void setThemes(int themes) {
		Config.THEMES = themes;
		if (Config.THEMES == 1) {
			check_fruit.setChecked(true);
		} else {
			check_fruit.setChecked(false);
		}

		if (Config.THEMES == 2) {
			check_trans.setChecked(true);
		} else {
			check_trans.setChecked(false);
		}

		if (Config.THEMES == 3) {
			check_animal.setChecked(true);
		} else {
			check_animal.setChecked(false);
		}

		if (Config.THEMES == 4) {
			check_math.setChecked(true);
		} else {
			check_math.setChecked(false);
		}

		mySharedPreferences.updateThemes(themes);

	}

	public void setDo_kho(int do_kho) {
		Config.Do_kho = do_kho;
		if (Config.Do_kho == 1) {
			check_de.setChecked(true);
		} else {
			check_de.setChecked(false);
		}

		if (Config.Do_kho == 2) {
			check_trungbinh.setChecked(true);
		} else {
			check_trungbinh.setChecked(false);
		}

		if (Config.Do_kho == 3) {
			check_kho.setChecked(true);
		} else {
			check_kho.setChecked(false);
		}
		mySharedPreferences.updateDo_kho(do_kho);

	}

	@Override
	public void onBackPressed() {
		Main.click.start();
		this.finish();
	}

}